
<template>
  <div>
    <div style="margin-bottom: 20px">
      <el-button type="primary" @click="addOrUpdateHandle(undefined)"
        >新增分类</el-button
      >
    </div>

    <el-tree
      :data="treeData"
      :props="defaultProps"
      :default-expanded-keys="defaultkeyarr"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span style="">
          <el-button
            type="text"
            size="mini"
            @click="addOrUpdateHandle(data.id, data.parentId)"
          >
            编辑
          </el-button>
          <el-button
            v-if="node.level != 3"
            type="text"
            size="mini"
            @click="addOrUpdateHandle(undefined, data.parentId,data.id)"
          >
            添加
          </el-button>
          <el-button
            v-if="data.children.length == 0"
            type="text"
            size="mini"
            @click="deleteHandle(data.id)"
          >
            删除
          </el-button>
        </span>
      </span>
    </el-tree>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getTree"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./catagory-add-or-update";
export default {
  data() {
    return {
      defaultkeyarr: [], // 默认展开的数组
      treeData: [],
      dataForm: {
        key: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
    };
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getDataList();
    this.getTree();
  },
  methods: {
    fnnodepand(node) {
      this.defaultkeyarr.push(node.id);
    },
    edit(data, node) {
      console.log(data, node);
    },
    append(data, node) {
      console.log(data, node);
    },
    getTree() {
      this.$http({
        url: this.$http.adornUrl("/testing/catagory/tree"),
        method: "get",
      }).then(({ data }) => {
        this.treeData = data.tree;
        console.log(data);
      });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/testing/catagory/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id, parentId,saveId) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, parentId,saveId);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/testing/catagory/delete"),
          method: "post",
          data: this.$http.adornData(ids, false),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getTree();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  },
};
</script>
